package com.inspur.cloud.dao.dataspace;

import com.inspur.cloud.entity.dataspace.HbaseResourceEntity;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import java.util.List;

public interface HbaseResourceDao extends JpaRepository<HbaseResourceEntity, Integer>, JpaSpecificationExecutor<HbaseResourceEntity> {
    HbaseResourceEntity findHbaseResourceEntityById(Integer id);
    @Query("select h from HbaseResourceEntity h where spaceName=:spaceName AND parentId=0 ")
    HbaseResourceEntity findBySpaceName(@Param("spaceName") String spaceName);

    @Query("select h from HbaseResourceEntity h where owner=:owner AND parentId=0 ")
    HbaseResourceEntity findFirstByOwner(@Param("owner") Integer owner);

    HbaseResourceEntity findByTableNameAndParentId(String tableName, Integer parentId);

    HbaseResourceEntity findByColumnNameAndParentId(String columnName, Integer parentId);

    HbaseResourceEntity findByColumnFamilyNameAndParentId(String columnName, Integer parentId);

    @Query("select count(h) from HbaseResourceEntity h where parentId=:id ")
    Integer findSonNum(@Param("id") Integer id);

    List<HbaseResourceEntity> findByParentId(Integer parentId);

    @Query("select h.id from HbaseResourceEntity h where  parentId=:parentId ")
    List<Integer> findIdByParentId(@Param("parentId") Integer parentId);

    @Query("delete from HbaseResourceEntity h where h.parentId=:parentId")
    @Modifying
    Integer deleteByParentId(@Param("parentId") Integer parentId);

    List<HbaseResourceEntity> findByLevelAndStatus(Integer level, Integer status);

    List<HbaseResourceEntity> findByLevelAndStatusAndOwner(Integer level, Integer status, Integer owner);

    @Query("update HbaseResourceEntity h  set h.status=0  where h.parentId=:parentId")
    @Modifying
    Integer hideByParentId(@Param("parentId") Integer parentId);

}
